<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="page-search layui-form" lay-filter="search-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">治疗单类别</label>
            <div class="layui-input-inline">
                <select id="type" name="typeId">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">项目名称</label>
            <div class="layui-input-inline">
                <input name="search-key" placeholder="请输入项目名称" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="search-submit">查询</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn create-btn">新建</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="table" lay-filter="table"></table>
<div id="pagination"></div>
</body>
<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-xs" lay-event="update">更新</button>
    {{#  if(d.disable){ }}
    <input type="checkbox" name="status" lay-skin="switch" data-project-id={{d.id}} lay-filter="status">
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" checked data-project-id={{d.id}} lay-filter="status">
    {{#  } }}
</script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['table', 'form', 'laypage', 'conf', 'utils', 'mandyServices'], function (table, form, laypage, config, utils, mandyServices) {
        var $createBtn = $('.create-btn');
        utils.aresPageEventInit('updateProjectList', 'project', function () {
            getPageList(1, config.pagination.limit);
        })
        getTypes();
        getPageList(1, config.pagination.limit);
        form.on('submit(search-submit)', function (data) {
            getPageList(1, config.pagination.limit);
            return false;
        });

        //启用禁用
        form.on('switch(status)', function (data) {
            enableProfile($(data.elem).data('projectId'), data.elem.checked).catch(function (err) {
                data.elem.checked = !data.elem.checked;
                form.render();
            })
        });

        $createBtn.on('click', function () {
            showPersonModal('新建项目')
            return false;
        })

        function showPersonModal(title, id) {
            var queryStr = (typeof id == 'string' || typeof id == 'number') ? '?id=' + id : '';
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/system/project_form.html' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        function handlerUpdate(data) {
            showPersonModal('更新项目', data.id);
        }

        //获取处方类别
        function getTypes() {
            return mandyServices.mandy_get_prescriptionType_syn().then(function (res) {
                var prcpType = $('select[name="typeId"]'),
                    data = res.data;
                prcpType.empty();
                prcpType.append('<option value="">全部</option>');
                $.each(data.prcp_type_list, function (idx, val) {
                    var option = $("<option>");
                    option.text(val.name);
                    option.attr("value", val.id);
                    prcpType.append(option);
                })
                form.render('select');
            })
        }

        //获取项目列表
        function getPageList(currentPage, pageSize) {
            var searchKey = $('input[name="search-key"]').val(),
                typeId = form.val('search-form').typeId;
            mandyServices.mandy_get_subitem_list_by_page(searchKey, typeId, currentPage, pageSize).then(function (res) {
                var data = res.data;
                laypage.render($.extend(config.pagination, {
                    count: data.count,
                    curr: data.current_page,
                    jump: function (obj, first) {
                        if (!first) getPageList(obj.curr, obj.limit);
                    }
                }));
                table.render({
                    elem: '#table',
                    data: data.list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '治疗单类别', field: 'typeName'},
                        {title: '项目名称', field: 'itemName', width: 300},
                        {title: '编码', field: 'itemCode'},
                        {title: '治疗时间(分钟)', field: 'treatTime'},
                        {title: '价格(元)', field: 'unitFee'},
                        {title: '创建时间', field: 'createTime', width: 130},
                        {
                            title: '是否使用设备',
                            templet: function (d) {
                                return utils.setEnableOrDisable(d.isUse, '是|否');
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#tableBar'}
                    ]]
                });
                table.on('tool(table)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'update':
                            handlerUpdate(data)
                            break;
                    }
                    ;
                });
            });
        }

        //启用禁用项目
        function enableProfile(projectId, checked) {
            mandyServices.mandy_update_subitem_disabled(projectId, !checked).then(function (res) {
                utils.msgSuccess(res.message);
            });
        }

    })
</script>


</html>